/**
 * All rights Reserved, Copyright (C) HAOWU LIMITED 2011-2014
 * FileName: CustomViewPager.java
 * Version:  $Revision$
 * Modify record:
 * NO. |		Date		|		Name		|		Content
 * 1   |	2014年12月8日		|	HAOWU)RenJie|	original version
 */

package com.zx.kitchen.index.common.view.viewpager;

import android.content.Context;
import android.os.CountDownTimer;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Interpolator;
import android.widget.Scroller;

import com.nineoldandroids.view.ViewHelper;

import java.lang.reflect.Field;

/**
 * class name:CustomViewPager <BR>
 * class description: please write your description <BR>
 * Remark: <BR>
 *
 * @author HAOWU)RenJie
 * @version 1.00 2014年12月8日
 */
public class AutoViewPager extends ViewPager implements OnPageChangeListener {

    private final int CHOOSE_TIME = 3000;
    boolean isIdle = false;
    private int CAROUSE_TIME = Integer.MAX_VALUE;
    private int VIEW_INDEX = 0;
    private CountDownTimer carouselTimer;
    private CountDownTimer fingerTimer;
    private MyScroller mScroller;

    public AutoViewPager(Context context) {
        super(context);
        // setPageTransformer(true, new DepthPageTransformer());
        // setCurrentAnimTime();
    }

    public AutoViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
        // setPageTransformer(true, new DepthPageTransformer());
        // setCurrentAnimTime();
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                CustomViewPagerInMain.setCanScroll(false);
                break;
            case MotionEvent.ACTION_UP:
                CustomViewPagerInMain.setCanScroll(true);
                // startCarousel();
                break;
            case MotionEvent.ACTION_MOVE:
                isIdle = true;
                if (carouselTimer != null)
                    carouselTimer.cancel();
                break;
            case MotionEvent.ACTION_CANCEL:
                CustomViewPagerInMain.setCanScroll(true);
                break;
            default:
                break;
        }
        return super.onTouchEvent(ev);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                CustomViewPagerInMain.setCanScroll(false);
                break;
            case MotionEvent.ACTION_UP:
                CustomViewPagerInMain.setCanScroll(true);
                break;
            case MotionEvent.ACTION_CANCEL:
                CustomViewPagerInMain.setCanScroll(true);
                break;
            default:
                break;
        }
        return super.onInterceptTouchEvent(event);
    }

    public void startCarousel() {
        if (fingerTimer != null)
            fingerTimer.cancel();
        fingerTimer = new CountDownTimer(3000, 50) {

            @Override
            public void onTick(long millisUntilFinished) {
                if (isIdle) {
                    isIdle = false;
                    fingerTimer.cancel();
                }
            }

            @Override
            public void onFinish() {
                IndeedStartCarousel();
            }
        }.start();
    }

    public void IndeedStartCarousel() {
        if (carouselTimer != null)
            carouselTimer.cancel();
        carouselTimer = new CountDownTimer(CAROUSE_TIME, CHOOSE_TIME) {

            @Override
            public void onTick(long millisUntilFinished) {
                if (millisUntilFinished <= 3000)
                    CAROUSE_TIME = Integer.MAX_VALUE;
                if (VIEW_INDEX >= getAdapter().getCount())
                    VIEW_INDEX = 0;
                // if (mScroller != null)
                // mScroller.setDuration(0);
                // setPageTransformer(false, null);
                // } else {
                // if (mScroller != null)
                // mScroller.setDuration(1000);
                // setPageTransformer(false, new DepthPageTransformer());
                setCurrentItem(VIEW_INDEX++, true);
            }

            @Override
            public void onFinish() {
            }
        }.start();
    }

    @Override
    public void onPageSelected(int arg0) {
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        super.onPageScrolled(position, positionOffset, positionOffsetPixels);
        VIEW_INDEX = position;

    }

    public void setCurrentAnimTime() {
        try {
            Field mField = ViewPager.class.getDeclaredField("mScroller");
            mField.setAccessible(true);
            mScroller = new MyScroller(getContext(), new AccelerateInterpolator());
            mField.set(this, mScroller);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setPageTransformer(boolean arg0, PageTransformer arg1) {
        super.setPageTransformer(arg0, arg1);
    }

    public class RotateDownPageTransformer implements PageTransformer {

        private static final float ROT_MAX = 20.0f;
        private float mRot;

        public void transformPage(View view, float position) {

        }
    }

    public class MyScroller extends Scroller {

        private int animTime = 0;

        public MyScroller(Context context) {
            super(context);
        }

        public MyScroller(Context context, Interpolator interpolator) {
            super(context, interpolator);
        }

        @Override
        public void startScroll(int startX, int startY, int dx, int dy, int duration) {
            if (animTime == 0)
                super.startScroll(startX, startY, dx, dy, duration);
            else
                super.startScroll(startX, startY, dx, dy, animTime);
            animTime = 0;
        }

        public void setDuration(int animTime) {
            this.animTime = animTime;
        }

    }

    public class DepthPageTransformer implements PageTransformer {

        private static final float MIN_SCALE = 0.75f;

        @Override
        public void transformPage(View view, float position) {
            int pageWidth = view.getWidth();
            if (position < -1) {
                ViewHelper.setAlpha(view, 0);
            } else if (position <= 0) {
                ViewHelper.setAlpha(view, 1);
                ViewHelper.setTranslationX(view, 0);
                ViewHelper.setScaleX(view, 1);
                ViewHelper.setScaleY(view, 1);
            } else if (position <= 1) {
                ViewHelper.setAlpha(view, 1 - position);
                ViewHelper.setTranslationX(view, pageWidth * -position);
                float scaleFactor = MIN_SCALE + (1 - MIN_SCALE) * (1 - Math.abs(position));
                ViewHelper.setScaleX(view, scaleFactor);
                ViewHelper.setScaleY(view, scaleFactor);
            } else {
                ViewHelper.setAlpha(view, 0);

            }
        }

    }

}
